package com.brkj.communityStudy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_TopicStudy;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseActionBarActivity;
import com.brkj.util.view.BaseActionBarItem;
import com.brkj.util.view.BaseCoreActivity;
import com.dgl.sdk.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/16.
 */
public class TopicStudyActivity extends BaseActionBarActivity{

    private PullListView listView;
    private TextView noData;
    private LinearLayout systemView;

    private List<DS_TopicStudy> topicList;    // 所有专题
    private List<DS_TopicStudy> myTopicList;  //我的专题
    private List<DS_TopicStudy> classTopicList;  //体系专题

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.topic_study);

        listView = (PullListView) findViewById(R.id.listView);
        noData = (TextView) findViewById(R.id.noData);
        systemView = (LinearLayout) findViewById(R.id.systemView);

        noData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTopic();
            }
        });

        initTitle();

        getTopic();

        getMyTopic();

        listView.hideFooterView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(TopicStudyActivity.this, TopicStudyDetailActivity.class);
                intent.putExtra("topicID", Integer.toString(topicList.get(position).getTpiocID()) );
                startActivity(intent);

            }
        });


        SystemStruct systemStruct = new SystemStruct(this, new SystemStruct.SystemStructItemClick() {
            @Override
            public void onClick(int classID, String systemName) {
                getTopicByID(classID);
            }
        });

        systemView.addView(systemStruct.getView());
    }

    private void initTitle() {
        setReturnBtn();

        addTitleButtonItem("专题学习", new BaseActionBarItem.itemClick() {
            @Override
            public void onClick() {
                if((topicList!= null) && (topicList.size()>0)){
                    TopicStudyAdapter adapter = new TopicStudyAdapter(TopicStudyActivity.this, topicList, false);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                    systemView.setVisibility(View.VISIBLE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getTopic();
                        }
                    });

                }
            }
        });

        addTitleButtonItem("我的专题", new BaseActionBarItem.itemClick() {
            @Override
            public void onClick() {

                if((myTopicList!= null) && (myTopicList.size()>0)){
                    TopicStudyAdapter adapter = new TopicStudyAdapter(TopicStudyActivity.this, myTopicList, true);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                    systemView.setVisibility(View.VISIBLE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getMyTopic();
                        }
                    });
                }

            }
        });


        addTitleButtonItem("专题体系", new BaseActionBarItem.itemClick() {
            @Override
            public void onClick() {
                listView.setVisibility(View.GONE);
                noData.setVisibility(View.GONE);
                systemView.setVisibility(View.VISIBLE);
            }
        });
    }

    public void getMyTopic() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetMyTopicStudy.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetMyTopicStudy.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)TopicStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_TopicStudy>>(){}.getType();
                myTopicList = gson.fromJson((String)t, listType);

            }
        });

    }


    public void getTopic() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetTopicStudy.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetTopicStudy.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)TopicStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_TopicStudy>>(){}.getType();
                topicList = gson.fromJson((String)t, listType);

                if((topicList!= null) && (topicList.size()>0)){
                    TopicStudyAdapter adapter = new TopicStudyAdapter(TopicStudyActivity.this, topicList, false);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);

                }


            }
        });

    }


    public void getTopicByID(final int classID) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetTopicStudyByID.params.UserID, MyApplication.myUserID);
        params.put(HttpInterface.HIF_GetTopicStudyByID.params.ClassInfoID, Integer.toString(classID));

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetTopicStudyByID.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)TopicStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_TopicStudy>>(){}.getType();
                classTopicList = gson.fromJson((String)t, listType);

                if((classTopicList!= null) && (classTopicList.size()>0)){
                    TopicStudyAdapter adapter = new TopicStudyAdapter(TopicStudyActivity.this, classTopicList, false);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getTopicByID(classID);
                        }
                    });
                }


            }
        });

    }


}
